package datas

import (
	"gin-bms/model"
	"os"
	"time"

	"github.com/gookit/color"
	"gorm.io/gorm"
)

var roles = []model.SysRole{
	{ CreatedAt: time.Now(), UpdatedAt: time.Now(), RoleId: "1", RoleName: "超级管理员", ParentId: "0"},
	{ CreatedAt: time.Now(), UpdatedAt: time.Now(), RoleId: "2", RoleName: "测试用户", ParentId: "0"},
	{ CreatedAt: time.Now(), UpdatedAt: time.Now(), RoleId: "3", RoleName: "普通用户", ParentId: "0"},
}

func InitSysRole(db *gorm.DB) {
	if err := db.Transaction(func(tx *gorm.DB) error {
		if tx.Where("role_id IN ?", []string{"1", "2", "3"}).Find(&[]model.SysRole{}).RowsAffected == 2 {
			color.Danger.Println("sys_roles表的初始化数据已存在")
			return nil
		}
		if err := tx.Create(&roles).Error; err != nil {
			return err // 遇到错误时回滚事务
		}
		return nil
	}); err != nil {
		color.Warn.Printf("[Mysql]-->sys_roles表的初始化失败, err:%v\n", err)
		os.Exit(0)
	}
}
